﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ElectronicJournal.DB;
using ElectronicJournal.DB.Item;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Threading;

namespace ElectronicJournal.Admin
{
    public partial class News : System.Web.UI.Page
    {
        int idNews;
        DataTable dt;
        string Tilte;
        string Content;

        protected void Page_Load(object sender, EventArgs e)
        {
            idNews = 0;
            try
            {

                idNews = Convert.ToInt32(Request["idNews"]);

                if (idNews > 0 && Session["update"] == null)
                {
                    DbNews dbNews = new DbNews();

                    ItemNews news = dbNews.GetNews(idNews);
                    txtTitle.Text = news.title;
                    txtContent.Text = news.content;

                    Session["update"] = true;
                }
            }
            catch (Exception ex)
            {
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ItemNews news = new ItemNews()
            {
                title = txtTitle.Text,
                content = txtContent.Text,
                date = DateTime.Now
            };

            DbNews dbNews = new DbNews();

            if (idNews > 0)
            {
                news.idNews = idNews;
                dbNews.Update(news);

                Session.Remove("update");
            }
            else
            {
                dbNews.Add(news);
            }

            // Sent to mails      

            // Lay du lieu Email tu CSDL
            SqlDb dbsqls = new SqlDb();
            string ReadMail = "SELECT Email FROM aspnet_Membership";
            dt = dbsqls.Read(ReadMail);
            // Lay du lieu news tu Database
            Tilte = news.title;
            Content = news.content;

            
            Thread thrd = new Thread(new ThreadStart(ThreadSendMail));
            thrd.Start();
            // Gui den mail cua readers

            Response.Redirect("~/Admin/News.aspx");
        }
        public void ThreadSendMail()
        {
            int rowCount = dt.Rows.Count;
            int i = 0;
            string emails;
            while (i < rowCount)
            {
                emails = dt.Rows[i].Field<string>(0);
                if (!SendMail_News(emails, Tilte, Content))
                    return; // false;
                i++;
            }
        }
        public bool SendMail_News(string to, string title, string contents)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(new MailAddress(to));
            mail.From = new MailAddress("phuan.tran1989@gmail.com");
            mail.Subject = title;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
            mail.Body = contents;
            try
            {
                SmtpClient client = new SmtpClient();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Host = "smtp.gmail.com"; // Хост Google для рассылки новостей по почте
                client.Port = 587; // Порт Google для рассылки новостей по почте

                NetworkCredential credentials = new System.Net.NetworkCredential("phuan.tran1989", "01011989pa"); // Mail of the administrator
                client.Credentials = credentials;

                client.Send(mail);

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }

        protected void txtContent_TextChanged(object sender, EventArgs e)
        {

        }
    }
}